/**
 * @api {get} /api/admin/v1/responses/browse 回复列表
 * @apiGroup AdminResponses
 * @apiDescription 回复管理-回复列表
 * @apiPermission 管理员(查看回复权限)
 * @apiVersion 1.0.0
 */
exports.browse = function ({query}, {models: {AutoResponses}, controller}) {
    return AutoResponses.findAndCountAll(controller.pagination({
        params: query,
        filters: ['level', 'status'],
        likes: ['keyword']
    }));
}

/**
 * @api {get} /api/admin/v1/responses/load 回复详情
 * @apiGroup AdminResponses
 * @apiDescription 回复管理-回复详情
 * @apiPermission 管理员(查看回复权限)
 * @apiVersion 1.0.0
 */
exports.load = function ({query}, {models: {AutoResponses}}) {
    return AutoResponses.findOne({where: {id: query.id}});
}

/**
 * @api {post} /api/admin/v1/responses/add 回复添加
 * @apiGroup AdminResponses
 * @apiDescription 回复管理-回复添加
 * @apiPermission 管理员(添加回复权限)
 * @apiVersion 1.0.0
 */
exports.add = function ({body}, {models: {AutoResponses}}) {
    return AutoResponses.create(body).then(function (data) {
        return {success: true, data: data};
    });
}

/**
 * @api {post} /api/admin/v1/responses/edit 回复编辑
 * @apiGroup AdminResponses
 * @apiDescription 回复管理-回复编辑
 * @apiPermission 管理员(编辑回复权限)
 * @apiVersion 1.0.0
 */
exports.edit = function ({body}, {models: {AutoResponses}}) {
    let {id} = body;
    delete body.id;
    return AutoResponses.update(body, {where: {id: id}}).then(function (result) {
        if (!result[0]) {
            return {success: false};
        }
        return AutoResponses.findOne({where: {id: id}}).then(function (response) {
            return {success: true, data: response};
        });
    });
}

/**
 * @api {post} /api/admin/v1/responses/delete 回复删除
 * @apiGroup AdminResponses
 * @apiDescription 回复管理-回复删除
 * @apiPermission 管理员(删除回复权限)
 * @apiVersion 1.0.0
 */
exports.delete = function ({body}, {models: {AutoResponses}}) {
    let {id} = body;
    return AutoResponses.destroy({where: {id: id}}).then(function (count) {
        return {success: (count > 0)};
    });
}